﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_thongtinKH : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            danhsach(0);
            sinhnhat();
        }
    }
    protected void danhsach(int index)
    {
        clsTableAction clsTable = new clsTableAction("ThongTin_KH");
        clsTable.exportDataToGridview("*", "", ref GridView1, 5, index);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        danhsach(e.NewPageIndex);
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        clsTableAction clsTable = new clsTableAction("ThongTin_KH");
        string id = GridView1.Rows[GridView1.EditIndex].Cells[0].Text;
        TextBox txt1 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[1].Controls[0];
        string lableVi2 = txt1.Text;
        TextBox txt2 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[2].Controls[0];
        string lableVi3 = txt2.Text;
        TextBox txt3 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[3].Controls[0];
        string lableVi4 = txt3.Text;
        TextBox txt4 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[4].Controls[0];
        string lableVi5 = txt4.Text;
        TextBox txt5 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[5].Controls[0];
        string lableVi6 = txt5.Text;
        TextBox txt6 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[6].Controls[0];
        string lableVi7 = txt6.Text;
        TextBox txt7 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[7].Controls[0];
        string lableVi8 = txt7.Text;
        TextBox txt8 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[8].Controls[0];
        string lableVi9 = txt8.Text;
        TextBox txt9 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[9].Controls[0];
        string lableVi10 = txt9.Text;
        //Tien hanh update
        string sql = string.Format("update ThongTin_KH set HoTen='{0}', MaNV='{1}', GioiTinh ='{2}', NgaySinh = '{3}', DiaChi='{4}', Sdt='{5}', Email='{6}', SoThich='{7}', NgheNghiep ='{8}', NgayDen ='{9}' where MaKH='{10}'", txt1.Text, txt2.Text, txt3.Text, txt4.Text, txt5.Text, txt6.Text, txt7.Text, txt8.Text,txt9.Text, DateTime.Now, id);
        if (clsTable.runSQL(sql))
        {
            GridView1.EditIndex = -1;
            danhsach(GridView1.PageIndex);
            tbl.Text = "Bạn sữa thành công";
        }
        else { tbl.Text = "that bai"; }
        clsTable.free();
    }
    protected void ThemKH_Click(object sender, EventArgs e)
    {
        clsTableAction clsTable = new clsTableAction("ThongTin_KH");
        string str = string.Format("select * from ThongTin_KH where MaKH='" + TextBox1.Text + "'");
        int kq = 0;
        //string sql = "select max(id_p) from page";
        //string id = (clsTable.runReturnSQL(sql) + 1).ToString();

        clsTable.runSQL(str, out kq);
        if (kq > 0)
        {
            Label12.Text = "Mã Khách Hàng đã tồn tại";
            TextBox1.Text = "";
            //return;
        }
        string sql = string.Format("insert into thongtin_KH values('{0}','{1}',N'{2}','{3}',N'{4}','{5}','{6}','{7}','{8}','{9}','{10}');", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text,TextBox10.Text, DateTime.Now);

        if (clsTable.runSQL(sql))
        {
            GridView1.EditIndex = -1;
            danhsach(0);
            Label12.Text = "Tin bạn thêm thành công";
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox4.Text = "";
            TextBox3.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = ""; 
            TextBox9.Text = "";
            TextBox10.Text = "";
            TextBox11.Text = "";
        }
        else
        {
            Label12.Text = "Tin bạn thêm không thành công";
        }
        clsTable.free();
    }
    protected void XoaKH_Click(object sender, EventArgs e)
    {
        clsQueryList clsQuery = new clsQueryList();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[9].FindControl("chkXoa");
            if (chk.Checked)
            {
                string id = GridView1.Rows[i].Cells[0].Text;
                string sql = "delete from ThongTin_KH where MaKH='" + id + "'";
                clsQuery.addSQL(sql);
            }
        }
        if (clsQuery.Execute())
        {
            danhsach(GridView1.PageIndex);
            tbl.Text = ("Bạn đã xóa thành công");
        }
        else
            tbl.Text = ("Xóa thành công");
    }
    protected void sinhnhat()
    {
        clsTableAction clsTable = new clsTableAction("ThongTin_KH");
        string str = string.Format("select * from ThongTin_KH where NgayLe='" + DateTime.Now + "'");
        bool kq = false;
        kq = clsTable.ktrunSQL(str);
        if (kq == false)
        {
            Label13.Text = "hnay sinh nhat ban";
            TextBox1.Text = "";
            return;
        }
    }
}